package com.asdan.seededu.web.controller;




import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.asdan.seededu.entity.security.SubProject;
import com.asdan.seededu.entity.security.User;
import com.asdan.seededu.front.service.UserInformationService;
import com.asdan.seededu.front.service.impl.Product01ServiceImp;
import com.asdan.seededu.front.service.impl.UserInfoServiceImp;
import com.asdan.seededu.util.Pager;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;

@Controller
@MenuSetting("menu-user")
@RequestMapping("userInfo")
public class UserInformationController {
	
	@RequestMapping(value="/userProInfo",method=RequestMethod.GET)
	public String userProInfo(String pageNo,String message ,Model model,HttpServletRequest req) {
		String uid =(String)req.getSession().getAttribute("userid") ;
		int Uid=0;
		if(uid!=null &&!"".equals(uid)){
			Uid=Integer.parseInt(uid);
		}
		if(pageNo==null||pageNo.equals("")){
			pageNo = "1";
		}
		UserInformationService us = new UserInfoServiceImp() ;
		List<SubProject> sublist=us.userProInfo(Uid); 
		for(int i=0;i<sublist.size();i++){
			System.out.println("sublist.get(i).getSpid() "+sublist.get(i).getSpid());
			sublist.get(i).setStepid(new Product01ServiceImp().getStepidbySpid(sublist.get(i).getSpid())) ;
		}
		int fpid=0;
		fpid=us.getWYEFid();
		int isweepfpid=0;
		isweepfpid=us.getISWEEPId();
		System.out.println(fpid+"fpid");
		model.addAttribute("sublist",sublist);
		//model.addAttribute("username",name);
		model.addAttribute("uid",uid);
		Pager<SubProject> page = us.getOrders(Uid, Integer.parseInt(pageNo)); //这块逻辑有问题吧?
		if(message!=null&&!"".equals(message)){
			model.addAttribute("message",message) ;
		}
		model.addAttribute("list", page.getDatas());
		model.addAttribute("pageNo",page.getOffset());
		model.addAttribute("pageCount", page.getTotalPage());
		model.addAttribute("totalCount",page.getTotal());
		model.addAttribute("size", page.getSize()) ;
		model.addAttribute("wyeffpid",fpid);
		model.addAttribute("ISWEEPfpid",isweepfpid);
		return "my_Center";
	}
	
	@RequestMapping(value="/{oid}/delorder",method=RequestMethod.GET)
	public String delorder(@PathVariable String oid,Model model,HttpServletRequest req) {
		String uid =(String)req.getSession().getAttribute("userid") ;
		int Oid=0;
		if(oid!=null &&!"".equals(oid)){
			Oid=Integer.parseInt(oid);
		}
		
		UserInformationService us = new UserInfoServiceImp() ;
		us.delorder(Oid);
		model.addAttribute("message","删除成功");
		model.addAttribute("uid",uid);
		return this.userProInfo("","", model,req);
	}
	
	@RequestMapping(value="/userOrder",method=RequestMethod.GET)
	public String getOrder(String pageNo,Model model,HttpServletRequest req) {
		String uid =(String)req.getSession().getAttribute("userid") ;
		int Uid=0;
		if(uid!=null &&!"".equals(uid)){
			Uid=Integer.parseInt(uid);
		}
		if(pageNo==null||pageNo.equals("")){
			pageNo = "1";
		}
		UserInformationService us = new UserInfoServiceImp() ;
		
		Pager<SubProject> page = us.getOrders(Uid, Integer.parseInt(pageNo));
		model.addAttribute("list", page.getDatas());
		model.addAttribute("pageNo",page.getOffset());
		model.addAttribute("pageCount", page.getTotalPage());
		model.addAttribute("totalCount",page.getTotal());
		model.addAttribute("size", page.getSize()) ;
		
		
		model.addAttribute("uid",uid);
		return "front_Wyef/myOrder";
	}
	
	
	@RequestMapping(value="/reviseuser",method=RequestMethod.GET)
	public String updateUserInfo(Model model,HttpServletRequest req){
		UserInformationService us = new UserInfoServiceImp() ;
		String uid =(String)req.getSession().getAttribute("userid") ;
		int Uid=0;
		if(uid!=null &&!"".equals(uid)){
			Uid=Integer.parseInt(uid);
		}
		User user=new UserInfoServiceImp().getUser(Uid);
		model.addAttribute("user", user) ;
		return "reviseuser" ;
	}
	
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String update(User user ,Model model){
		UserInformationService us = new UserInfoServiceImp() ;
		boolean temp = us.updateUserInfo(user) ;
		User use=new UserInfoServiceImp().getUser(user.getUid());
		model.addAttribute("user",use) ;

		if(temp){
			model.addAttribute("message", "更改成功！") ;
		}else{
				model.addAttribute("message", "更改失败") ;
				return "reviseuser" ;
			   
		}
		return "product01" ;
	}
	
	

	
	
	@RequestMapping(value="/showOrderBypage",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String showOrderBypage(String uid,String pageNo,Model model){
		System.out.println(uid +"  "+pageNo) ;
		UserInformationService us = new UserInfoServiceImp() ;
		if(pageNo==null||pageNo.equals("")){
			pageNo = "1";
		}
		int uId=0;
		if(uid!=null &&!"".equals(uid)){
			 uId=Integer.parseInt(uid);
			 System.out.println("uid="+uId);
		}
		Pager<SubProject> page = us.getOrders(uId,Integer.parseInt(pageNo));
	    System.out.println(JsonUtils.toJsonString(page));
		return JsonUtils.toJsonString(page) ;
	}	
	
	
}
